﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class RewardDetails : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("SELECT * FROM Users WHERE u_id=@uid", conn);
            cmdSearch.Parameters.AddWithValue("@uid", Session["id"]);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            lblUserID.Text = dtrSearch["u_id"].ToString();
            lblPoints.Text = dtrSearch["u_points"].ToString();
            lblUserName.Text = dtrSearch["username"].ToString();
            dtrSearch.Close();

            cmdSearch = new SqlCommand("SELECT * FROM Reward WHERE r_id=@rid", conn);
            cmdSearch.Parameters.AddWithValue("@rid", Request.QueryString["id"]);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            lblRID.Text = dtrSearch["r_id"].ToString();
            lblName.Text = dtrSearch["r_name"].ToString();
            lblName2.Text = dtrSearch["r_name"].ToString();
            lblDesc.Text = dtrSearch["r_desc"].ToString();
            lblPointsNeed.Text = dtrSearch["r_points"].ToString();
            lblQuantity.Text = dtrSearch["r_quantity"].ToString();

            dtrSearch.Close();
            conn.Close();
        }

        public string getPic()
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select r_picture From Reward Where r_id=@id", conn);
            cmdSearch.Parameters.AddWithValue("@id", Request.QueryString["id"]);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            String pic = dtrSearch["r_picture"].ToString();
            conn.Close();

            return pic;
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnRedeem_Click(object sender, EventArgs e)
        {
            SqlCommand cmdSearchTID, cmdInsert, cmdInsert2;
            conn.Open();

            cmdSearchTID = new SqlCommand("Select MAX(t_id) from Transactions", conn);
            int t_id = Convert.ToInt32(cmdSearchTID.ExecuteScalar());
            t_id++;

            cmdInsert = new SqlCommand("Insert into Transactions Values(@TID,@UID,@SID,@Tseller,@T_Status,@T_Total)", conn);
            cmdInsert.Parameters.AddWithValue("@TID", t_id);
            cmdInsert.Parameters.AddWithValue("@UID", Convert.ToInt32(lblUserID.Text));
            cmdInsert.Parameters.AddWithValue("@SID", Convert.ToInt32("0"));
            cmdInsert.Parameters.AddWithValue("@Tseller", "HOB");
            cmdInsert.Parameters.AddWithValue("@T_Status", "Reward");
            cmdInsert.Parameters.AddWithValue("@T_Total", lblPointsNeed.Text);
            cmdInsert.ExecuteNonQuery();

            cmdInsert2 = new SqlCommand("Insert into Transactions_Products Values(@PID,@TID,@Quantity,@Date)", conn);
            cmdInsert2.Parameters.AddWithValue("@PID", Convert.ToInt32(lblRID.Text));
            cmdInsert2.Parameters.AddWithValue("@TID", Convert.ToInt32(t_id));
            cmdInsert2.Parameters.AddWithValue("@Quantity", 1);
            cmdInsert2.Parameters.AddWithValue("@Date", DateTime.Now);
            cmdInsert2.ExecuteNonQuery();
            conn.Close();

            Session["RewardReference"] = t_id;

            Response.Redirect("RewardShipping.aspx");
        }
    }
}